package com.nlx.notes.module.controller.file;


import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import com.alibaba.excel.EasyExcel;
import com.nlx.notes.module.bean.dto.file.excel.BillRecordDTO;
import com.nlx.notes.module.bean.dto.file.excel.ZfbBillRecordDTO;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.helper.sms.excel.BillExcelListener;
import com.nlx.notes.module.helper.sms.excel.ZfbBillExcelListener;
import com.nlx.notes.module.service.flow.FlowExcelService;
import com.nlx.notes.module.service.flow.FlowManageService;
import com.nlx.notes.support.context.UserHolder;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/file/excel")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class FlowExcelController {


    final FlowExcelService flowExcelService;


    @PostMapping("/upload")
    public boolean uploadExcel(@RequestParam("file") MultipartFile file) throws IOException {
        BillExcelListener listener = new BillExcelListener();
        EasyExcel.read(file.getInputStream(), BillRecordDTO.class, listener).sheet().doRead();
        List<BillRecordDTO> recordList = listener.getRecordList();
        log.info("获取到的数据：{}", recordList.size());
        return flowExcelService.saveFlowByExcelData(recordList);
    }


    @PostMapping("/upload-zfb")
    public boolean uploadZfbExcel(@RequestParam("file") MultipartFile file,@RequestParam("password") String password)  {
        flowExcelService.saveFlowByZfbExcelData(file,password, UserHolder.getUserContext().getId());
        return true;
    }


//    @PostConstruct
//    public void test() throws Exception {
//        File file = new File("D:\\文档\\备案\\cashbook_record_20250802_174049.csv");
//        List<ZfbBillRecordDTO> records = new ArrayList<>();
//
//        try (BufferedReader reader = new BufferedReader(
//                new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
//
//            String line;
//            int index = 1;
//            while ((line = reader.readLine()) != null) {
//                // 跳过表头
//                if (index <= 11) {
//                    index++;
//                    continue;
//                }
//                String[] columns = line.split(",", -1); // -1 允许空列
//                if (columns.length >= 8) {
//                    ZfbBillRecordDTO record = new ZfbBillRecordDTO(
//                            columns[0], columns[1], columns[2], columns[3],
//                            columns[4], columns[5], columns[6], columns[7]);
//                    records.add(record);
//                }
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            flowExcelService.saveFlowByZfbExcelData(records);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        System.out.println("总记录："+records.size());
//        // 打印结果
////        for (ZfbBillRecordDTO record : records) {
////            System.out.println(JSONUtils.toString(record));
////        }
//        System.out.println(records.stream().map(ZfbBillRecordDTO::getCategory).distinct().toList());
//    }


}
